<?php
/* 
 * @author Alexander.Chajka
 */
class database {
    private $cid;
    private $res;

    private $db_path;
    private $db_user;
    private $db_pass;
    private $db_base;
    private $db_prefix;

    public $_lastResult;

    public function __construct($options) {
        $this->db_path = $options->db_path;
        $this->db_user = $options->db_user;
        $this->db_pass = $options->db_pass;
        $this->db_base = $options->db_base;
        $this->db_prefix = $options->db_prefix;

        if (!$this->connect()) $this->_lastResult = -1;
    }

    private function replacePrefix($query) {
        return str_replace('#__', $this->db_prefix, $query);
    }

    /**
      * Connect to DB and select TABLE
      * @return executing $state true or false
      */
    public function connect() {
        $this->cid = mysql_pconnect($this->db_path, $this->db_user, $this->db_pass);
        if (!$this->cid) return false;

        if (!mysql_select_db($this->db_base, $this->cid)) return false;
        return true;
    }

    /**
      * Set and executing query
      * @param string $query
      * @return executing $state
      * <p>
      * if success return number of affected rows or false
      * </p>
      */
    public function query($query){
        $this->res = mysql_query($this->replacePrefix($query), $this->cid);
        
        if (mysql_error() != '') {
            $this->_lastResult = -1;
            return false;
        } else {
            $this->_lastResult = mysql_affected_rows();
            return $this->_lastResult;
        }
    }

    /**
      * Get first resulting object of query
      * @return object $result
      */
    public function getObject(){
        $res = mysql_query($query, $this->cid);
        if (mysql_error() != '') return false;

        $obj = mysql_fetch_object($res);
        if (!$obj || empty($obj)) return false;
        else return $obj;
    }

    /**
      * Get result objects array of query
      * @return objects array $result
      */
    public function getObjectsArray(){
        $res = mysql_query($query, $this->cid);
        if (mysql_error() != '') return false;

        $arr = false;
        while($obj = mysql_fetch_object($res)) {
            if (!$obj || empty($obj)) break; // опять ошибка или закончились записи
            $arr[] = $obj;
        }
        return $arr;
    }

    /**
      * Get result array of query
      * @return array $result
      */
    public function getArray(){
        $res = mysql_query($query, $this->cid);
        if (mysql_error() != '') return false;

        $arr = false;
        while($row = mysql_fetch_array($res, MYSQL_NUM)) {
            if (!$row || empty($row)) break; // опять ошибка или закончились записи
            $arr[] = $row[0];
        }
        return $arr;
    }

    /**
      * Get result field value of query
      * @return string $result
      */
    public function getResult(){
        $res = mysql_query($query, $this->cid);
        if (mysql_error() != '') return false;

        $arr = false;
        $row = mysql_fetch_array($res, MYSQL_NUM);
        if (!$row || empty($row)) return false; // опять ошибка или закончились записи
        else return $row[0];
    }
    
    public function error() {
        return mysql_errno().': '.mysql_error();
    }

    public function getLastError(){
        return mysql_error();
    }

    public function getLastErrorNum(){
        return mysql_errno();
    }

    public function getLastResult(){
        return $this->_lastResult;
    }
}
?>
